#pragma once
#include "MessagePubSubTypes.h"
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/topic/Topic.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/DataWriterListener.hpp>
#include <thread>
#include <unordered_map>
#include <iostream>
class Publisher_profile {
public:
    Publisher_profile();

    ~Publisher_profile();
    bool init(std::string xml_path, std::string participant_profile,
        std::string topic_profile, std::string datawriter_profile, std::string topic_name);

    //!Publish a sample
    bool publish(std::string topic_name,Message &msg);

    //!Run for number samples
    void run(std::string topic_name,
        uint32_t samples,
        uint32_t sleep,
        uint32_t send_size);
    void stop();
protected:

    Message message_;

    eprosima::fastdds::dds::DomainParticipant* participant_;

    eprosima::fastdds::dds::Publisher* publisher_;

    eprosima::fastdds::dds::Topic* topic_;

    eprosima::fastdds::dds::DataWriter* writer_;

    std::atomic_bool stop_;

    class PubListener : public eprosima::fastdds::dds::DataWriterListener
    {
    public:

        PubListener()
            : matched_(0)
            , firstConnected_(false), zeroCopy_(false)
        {
        }

        ~PubListener() override
        {
        }

        void on_publication_matched(
            eprosima::fastdds::dds::DataWriter* writer,
            const eprosima::fastdds::dds::PublicationMatchedStatus& info) override {
            std::cout << "publication matched." << writer->get_topic()->get_name() << std::endl;
        }

        int matched_;

        bool firstConnected_;
        bool zeroCopy_;
    }
    listener_;

    virtual void runThread(std::string topic_name,
        uint32_t samples,
        uint32_t sleep,
        uint32_t send_size);

    eprosima::fastdds::dds::TypeSupport type_;

private:
   // bool zeroCopyWriter(bool waitForListener);
    //bool genericWriter(bool waitForListener);
private:
    std::unordered_map<std::string, std::thread> threads_;
    std::unordered_map<std::string, eprosima::fastdds::dds::Publisher*> publishers_;
};